package com.qf.team2.netdisc.service.impl;

import com.qf.team2.netdisc.common.exception.BusinessException;
import com.qf.team2.netdisc.common.pojo.entity.Document;
import com.qf.team2.netdisc.common.pojo.entity.User;
import com.qf.team2.netdisc.common.pojo.qo.DocumentQO;
import com.qf.team2.netdisc.common.pojo.qo.UserQO;
import com.qf.team2.netdisc.common.util.MD5Util;
import com.qf.team2.netdisc.dao.UserMapper;
import com.qf.team2.netdisc.service.DocumentInsertService;
import com.qf.team2.netdisc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户服务接口实现类
 *
 * @author Administrator
 * @date 2021/05/22
 */
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DocumentInsertService documentInsertService;

    @Override
    public User userLogin(UserQO userQO) {
        //todo Service层涉及数据库操作的业务都要加@Transactional
        User dbUser = userMapper.selectUserByUsername(userQO.getUsername());
        if (null==dbUser){
            throw new BusinessException("用户不存在");
        }

        if (!dbUser.getPassword().equals(userQO.getPassword())) {
            throw new BusinessException("密码错误");
        }
        //todo Service层业务失败时应该抛出业务异常，而不是返回空值
        dbUser.setPassword("");
        return dbUser;
    }

    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public User registerByUserNameAndPassword(UserQO userQO) {
        if ("".equals(userQO.getUsername())
                || null == userQO.getUsername()
                || "".equals(userQO.getPassword())
                || null == userQO.getPassword()
                || "".equals(userQO.getEmail())
                || null == userQO.getEmail()
        ) {
            throw new BusinessException("用户信息不全");
        }
        User dbUser = userMapper.selectUserByUsername(userQO.getUsername());
        if (dbUser != null) {
            throw new BusinessException("用户已存在");
        }
        //注册用户
        String s = MD5Util.md5(userQO.getPassword());
        userQO.setPassword(s);
        userMapper.addUser(userQO);
        Integer userId = userQO.getId();
        String username = userQO.getUsername();
        //documentInsertService.documentInsert(username, userId);

        DocumentQO documentQO = new DocumentQO();
        documentQO.setFileName(username);
        documentQO.setUserId(userId);
        documentQO.setFileSize(0);

        userMapper.addDirectory(documentQO);
        Integer rootId = documentQO.getFileId();
        int result = userMapper.updateUserRoot(userId, rootId);
        if (result != 1) {
            throw new BusinessException("未知错误");
        }
        dbUser = userMapper.selectUserByUserId(userId);
        dbUser.setPassword("");
        return dbUser;
    }

    @Override
    public int userCapacity(String userName) {
        return userMapper.selectCapacityByUserName(userName);
    }

    @Override
    public User setUserVIP(Integer userId) {
        if (userMapper.updateCharacter(userId, User.CharacterCode.VIP_USER.getValue()) != 1) {
            throw new BusinessException("未知错误");
        }
        User dbUser = userMapper.selectUserByUserId(userId);
        if (dbUser == null) {
            throw new BusinessException("找不到该用户");
        }
        return dbUser;
    }
}
